import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by lenovo on 2018/11/7.
 */
public class BIOSocketServer {
    public static void main(String [] args){
        BIOSocketServer bioSocketServer=new BIOSocketServer();
        bioSocketServer.init();
    }

    public void init(){
        try {
            ServerSocket serverSocket=new ServerSocket(8080);

            while (true){
                Socket socket= serverSocket.accept() ;

                HandlerThread handlerThread= new HandlerThread(socket);
                new Thread(handlerThread).start();
            }


        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public class HandlerThread implements Runnable{

        private Socket socket=null;

        public HandlerThread(Socket socket){
            this.socket=socket;
        }

        @Override
        public void run() {
            try {

//                try {
//                    Thread.sleep(5000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//
                DataOutputStream outputStream= new DataOutputStream(socket.getOutputStream());
                outputStream.writeUTF("111111111111");
                outputStream.close();


                DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
                String clientInputStr = dataInputStream.readUTF();
                System.out.println("客户端发过来的内容:" + clientInputStr);


                DataOutputStream out = new DataOutputStream(socket.getOutputStream());
                System.out.print("请输入:\t");
                // 发送键盘输入的一行
                String s = new BufferedReader(new InputStreamReader(System.in)).readLine();
                out.writeUTF(s);

                out.close();
                dataInputStream.close();

            } catch (IOException e) {
                e.printStackTrace();
            } finally {

                if (socket != null) {
                    try {
                        socket.close();
                    } catch (IOException e) {
                        socket = null;
                    }
                }
            }
        }
    }
}
